package com.devunion.salon.web.action;

import com.devunion.salon.persistence.Employee;
import com.devunion.salon.persistence.Product;
import com.devunion.salon.persistence.Service;
import com.devunion.salon.persistence.dao.EmployeeDao;
import com.devunion.salon.persistence.dao.GiftCardDao;
import com.devunion.salon.persistence.dao.ProductDao;
import com.devunion.salon.persistence.dao.ServiceDao;
import com.devunion.salon.util.TransactionCodeGenerator;
import com.devunion.salon.web.bean.*;
import com.devunion.salon.web.form.TransactionForm;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;


/**
 * DOCUMENT ME!
 *
 * @author Timoshenko Alexander 12.09.2008 : 22:10:59
 */
public class TransactionAction extends CoreAction {
    private static final String start_Option = "<option value={0}>";
    private static final String end_Option = "</option>";

    private EmployeeDao employeeDao;
    private ProductDao productDao;
    private ServiceDao serviceDao;
    private GiftCardDao giftCardDao;

    public ActionForward welcome(ActionMapping actionMapping, ActionForm actionForm,
                                 HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
            throws Exception {
        TransactionForm form = (TransactionForm) actionForm;

        if (!StringUtils.isBlank(form.getSubmittedValue())
                && form.getSubmittedValue()
                .equalsIgnoreCase(resources.getMessage("add.transaction.button.add"))) {
            TransactionBean bean = new TransactionBean();
            Employee employee = employeeDao.getByKey(Long.valueOf(form.getEmployee()));
            bean.setEmployee(new EmployeeBean(employee));

            if (!StringUtils.isBlank(form.getSelectedProduct())) {
                Product product = productDao.getByKey(Long.valueOf(
                        form.getSelectedProduct()));
                bean.setProduct(new ProductBean(product));
            } else if (!StringUtils.isBlank(form.getSelectedService())) {
                Service service = serviceDao.getByKey(Long.valueOf(
                        form.getSelectedService()));
                bean.setService(new ServiceBean(service));
            }
            bean.setQuantity(1);
            if (form.getTransactions().contains(bean)) {
                int position = form.getTransactions().indexOf(bean);
                TransactionBean b = form.getTransactions().get(position);
                b.setQuantity(b.getQuantity() + 1);
            } else {
                bean.setTransNumber(TransactionCodeGenerator.next());
                form.getTransactions().add(bean);
            }

            return actionMapping.findForward("success");
        } else if (!StringUtils.isBlank(form.getDelete())) {
            for (int i = 0; i < form.getTransactions().size(); i++) {
                TransactionBean bean = form.getTransactions().get(i);
                if (bean.getTransNumber().equalsIgnoreCase(form.getDelete())) {
                    form.getTransactions().remove(i);
                    break;
                }
            }
            return actionMapping.findForward("success");
        }
        Collection<Employee> employees = employeeDao.getEmployees(getUsername());
        Collection<Product> products = productDao.getProducts(getUsername());

        form.getEmployees().clear();
        for (Employee employee : employees) {
            form.getEmployees()
                    .add(new LabelValueBean(employee.getFirstName() + " "
                            + employee.getLastName(), employee.getId().toString()));
        }

        form.getProducts().clear();
        for (Product product : products) {
            form.getProducts()
                    .add(new LabelValueBean(product.getName(), product.getId().toString()));
        }
        form.getTransactions().clear();
        return actionMapping.findForward("success");
    }

    public ActionForward giftcard(ActionMapping actionMapping, ActionForm actionForm,
                                  HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
            throws Exception {
        TransactionForm form = (TransactionForm) actionForm;

        TransactionBean bean = new TransactionBean();
        bean.setTransNumber(TransactionCodeGenerator.next());

        GiftcardBean giftcardBean = new GiftcardBean();
        giftcardBean.setAmount(Float.valueOf(form.getAmount()));
        giftcardBean.setTaxes(0f);
        giftcardBean.setName(resources.getMessage(
                "modal.add.transaction.payment.giftcard").toLowerCase());
        bean.setGiftcardBean(giftcardBean);
        bean.setQuantity(1);
        form.getTransactions().add(bean);


        return actionMapping.findForward("success");
    }

    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }

    public void setProductDao(ProductDao productDao) {
        this.productDao = productDao;
    }

    public void setServiceDao(ServiceDao serviceDao) {
        this.serviceDao = serviceDao;
    }

    public void setGiftCardDao(GiftCardDao giftCardDao) {
        this.giftCardDao = giftCardDao;
    }
}
